using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VMIBLG
    {
        public DataRow Convert2MIBLG(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryMIBLG(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["MIMANDANT"] = " ".PadRight(2 ); 
         dwReturn["MIJJJJ"] = " ".PadRight(0 ); 
         dwReturn["MIMM"] = " ".PadRight(0 ); 
         dwReturn["MITT"] = " ".PadRight(0 ); 
         dwReturn["MILO"] = " ".PadRight(2 ); 
         dwReturn["MILB"] = " ".PadRight(2 ); 
         dwReturn["MIINVBLG"] = " ".PadRight(0 ); 
         dwReturn["MIBLGPOS"] = " ".PadRight(0 ); 
         dwReturn["MILP"] = " ".PadRight(8 ); 
         dwReturn["MIAUFBKZ"] = " ".PadRight(1 ); 
         dwReturn["MICHAR"] = " ".PadRight(0 ); 
         dwReturn["MIGNR"] = " ".PadRight(0 ); 
         dwReturn["MIVPEINH"] = " ".PadRight(4 ); 
         dwReturn["MIGEBANZ"] = " ".PadRight(0 ); 
         dwReturn["MISME"] = " ".PadRight(0 ); 
         dwReturn["MIIME"] = " ".PadRight(0 ); 
         dwReturn["MIINBEN"] = " ".PadRight(8 ); 
         dwReturn["MIINDAT"] = " ".PadRight(0 ); 
         dwReturn["MIAENZL"] = " ".PadRight(0 ); 
         dwReturn["MIEJJJJ"] = " ".PadRight(0 ); 
         dwReturn["MIEBELEG"] = " ".PadRight(0 ); 
         dwReturn["MIELFDNR"] = " ".PadRight(0 ); 
         dwReturn["MILJJJJ"] = " ".PadRight(0 ); 
         dwReturn["MILBELEG"] = " ".PadRight(0 ); 
         dwReturn["MILLFDNR"] = " ".PadRight(0 ); 
         dwReturn["MIRUECKKZ"] = " ".PadRight(1 ); 
         dwReturn["MIMAT"] = " ".PadRight(14 ); 
         dwReturn["MIVAR"] = " ".PadRight(3 ); 
         dwReturn["MIEINDAT"] = " ".PadRight(0 ); 
         dwReturn["MISPSTA"] = " ".PadRight(2 ); 
         dwReturn["MISPGRD"] = " ".PadRight(2 ); 
         dwReturn["MILIEFNR"] = " ".PadRight(0 ); 
         dwReturn["MICHARL"] = " ".PadRight(15 ); 
         dwReturn["MIGNRL"] = " ".PadRight(15 ); 
         dwReturn["MIVERFDAT"] = " ".PadRight(0 ); 
         dwReturn["MIENTDAT"] = " ".PadRight(0 ); 
         dwReturn["MIENTKZ"] = " ".PadRight(1 ); 
         dwReturn["MILOESCHKZ"] = " ".PadRight(1 ); 
         dwReturn["MISORTKZ"] = " ".PadRight(1 ); 
         dwReturn["MIFILLER"] = " ".PadRight(34 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapMIBLG(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveMIBLG(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryMIBLG(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapMIBLG(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertMIBLG(dwLocal, ITXDBVERSION);
            }
            UpdateMIBLG(dwLocal);
        }
        public void InsertMIBLG(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO MIBLG(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryMIBLG(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM MIBLG WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM MIBLG  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "MIBLG";
            Return = Result;
            return Return;
        }
        public void DeleteMIBLG(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM MIBLG   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateMIBLG(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE MIBLG SET "
                    +"MIMANDANT= :MIMANDANT,"
                    +"MIJJJJ= :MIJJJJ,"
                    +"MIMM= :MIMM,"
                    +"MITT= :MITT,"
                    +"MILO= :MILO,"
                    +"MILB= :MILB,"
                    +"MIINVBLG= :MIINVBLG,"
                    +"MIBLGPOS= :MIBLGPOS,"
                    +"MILP= :MILP,"
                    +"MIAUFBKZ= :MIAUFBKZ,"
                    +"MICHAR= :MICHAR,"
                    +"MIGNR= :MIGNR,"
                    +"MIVPEINH= :MIVPEINH,"
                    +"MIGEBANZ= :MIGEBANZ,"
                    +"MISME= :MISME,"
                    +"MIIME= :MIIME,"
                    +"MIINBEN= :MIINBEN,"
                    +"MIINDAT= :MIINDAT,"
                    +"MIAENZL= :MIAENZL,"
                    +"MIEJJJJ= :MIEJJJJ,"
                    +"MIEBELEG= :MIEBELEG,"
                    +"MIELFDNR= :MIELFDNR,"
                    +"MILJJJJ= :MILJJJJ,"
                    +"MILBELEG= :MILBELEG,"
                    +"MILLFDNR= :MILLFDNR,"
                    +"MIRUECKKZ= :MIRUECKKZ,"
                    +"MIMAT= :MIMAT,"
                    +"MIVAR= :MIVAR,"
                    +"MIEINDAT= :MIEINDAT,"
                    +"MISPSTA= :MISPSTA,"
                    +"MISPGRD= :MISPGRD,"
                    +"MILIEFNR= :MILIEFNR,"
                    +"MICHARL= :MICHARL,"
                    +"MIGNRL= :MIGNRL,"
                    +"MIVERFDAT= :MIVERFDAT,"
                    +"MIENTDAT= :MIENTDAT,"
                    +"MIENTKZ= :MIENTKZ,"
                    +"MILOESCHKZ= :MILOESCHKZ,"
                    +"MISORTKZ= :MISORTKZ,"
                    +"MIFILLER= :MIFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("MIMANDANT", OracleType.NVarChar).Value = dwData["MIMANDANT"].ToString(); 
          or1.Parameters.Add("MIJJJJ", OracleType.NVarChar).Value = dwData["MIJJJJ"].ToString(); 
          or1.Parameters.Add("MIMM", OracleType.NVarChar).Value = dwData["MIMM"].ToString(); 
          or1.Parameters.Add("MITT", OracleType.NVarChar).Value = dwData["MITT"].ToString(); 
          or1.Parameters.Add("MILO", OracleType.NVarChar).Value = dwData["MILO"].ToString(); 
          or1.Parameters.Add("MILB", OracleType.NVarChar).Value = dwData["MILB"].ToString(); 
          or1.Parameters.Add("MIINVBLG", OracleType.NVarChar).Value = dwData["MIINVBLG"].ToString(); 
          or1.Parameters.Add("MIBLGPOS", OracleType.NVarChar).Value = dwData["MIBLGPOS"].ToString(); 
          or1.Parameters.Add("MILP", OracleType.NVarChar).Value = dwData["MILP"].ToString(); 
          or1.Parameters.Add("MIAUFBKZ", OracleType.NVarChar).Value = dwData["MIAUFBKZ"].ToString(); 
          or1.Parameters.Add("MICHAR", OracleType.NVarChar).Value = dwData["MICHAR"].ToString(); 
          or1.Parameters.Add("MIGNR", OracleType.NVarChar).Value = dwData["MIGNR"].ToString(); 
          or1.Parameters.Add("MIVPEINH", OracleType.NVarChar).Value = dwData["MIVPEINH"].ToString(); 
          or1.Parameters.Add("MIGEBANZ", OracleType.NVarChar).Value = dwData["MIGEBANZ"].ToString(); 
          or1.Parameters.Add("MISME", OracleType.NVarChar).Value = dwData["MISME"].ToString(); 
          or1.Parameters.Add("MIIME", OracleType.NVarChar).Value = dwData["MIIME"].ToString(); 
          or1.Parameters.Add("MIINBEN", OracleType.NVarChar).Value = dwData["MIINBEN"].ToString(); 
          or1.Parameters.Add("MIINDAT", OracleType.NVarChar).Value = dwData["MIINDAT"].ToString(); 
          or1.Parameters.Add("MIAENZL", OracleType.NVarChar).Value = dwData["MIAENZL"].ToString(); 
          or1.Parameters.Add("MIEJJJJ", OracleType.NVarChar).Value = dwData["MIEJJJJ"].ToString(); 
          or1.Parameters.Add("MIEBELEG", OracleType.NVarChar).Value = dwData["MIEBELEG"].ToString(); 
          or1.Parameters.Add("MIELFDNR", OracleType.NVarChar).Value = dwData["MIELFDNR"].ToString(); 
          or1.Parameters.Add("MILJJJJ", OracleType.NVarChar).Value = dwData["MILJJJJ"].ToString(); 
          or1.Parameters.Add("MILBELEG", OracleType.NVarChar).Value = dwData["MILBELEG"].ToString(); 
          or1.Parameters.Add("MILLFDNR", OracleType.NVarChar).Value = dwData["MILLFDNR"].ToString(); 
          or1.Parameters.Add("MIRUECKKZ", OracleType.NVarChar).Value = dwData["MIRUECKKZ"].ToString(); 
          or1.Parameters.Add("MIMAT", OracleType.NVarChar).Value = dwData["MIMAT"].ToString(); 
          or1.Parameters.Add("MIVAR", OracleType.NVarChar).Value = dwData["MIVAR"].ToString(); 
          or1.Parameters.Add("MIEINDAT", OracleType.NVarChar).Value = dwData["MIEINDAT"].ToString(); 
          or1.Parameters.Add("MISPSTA", OracleType.NVarChar).Value = dwData["MISPSTA"].ToString(); 
          or1.Parameters.Add("MISPGRD", OracleType.NVarChar).Value = dwData["MISPGRD"].ToString(); 
          or1.Parameters.Add("MILIEFNR", OracleType.NVarChar).Value = dwData["MILIEFNR"].ToString(); 
          or1.Parameters.Add("MICHARL", OracleType.NVarChar).Value = dwData["MICHARL"].ToString(); 
          or1.Parameters.Add("MIGNRL", OracleType.NVarChar).Value = dwData["MIGNRL"].ToString(); 
          or1.Parameters.Add("MIVERFDAT", OracleType.NVarChar).Value = dwData["MIVERFDAT"].ToString(); 
          or1.Parameters.Add("MIENTDAT", OracleType.NVarChar).Value = dwData["MIENTDAT"].ToString(); 
          or1.Parameters.Add("MIENTKZ", OracleType.NVarChar).Value = dwData["MIENTKZ"].ToString(); 
          or1.Parameters.Add("MILOESCHKZ", OracleType.NVarChar).Value = dwData["MILOESCHKZ"].ToString(); 
          or1.Parameters.Add("MISORTKZ", OracleType.NVarChar).Value = dwData["MISORTKZ"].ToString(); 
          or1.Parameters.Add("MIFILLER", OracleType.NVarChar).Value = dwData["MIFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
